import '@vant/touch-emulator'
import { Lazyload } from 'vant'
import Vue from 'vue'
import VueLazyLoad from 'vue-lazyload'
import App from './App.vue'
import './assets/font/DIN.css'
import './assets/font/iconfont.css'
import i18n from './i18n'
import router from './router'
import store from './store'
import './style/basics.less' // 公共CSS
import './style/reset.css' // 初始CSS
import { browser } from './util/browser' // 浏览器版本
import plugin from './util/plugin' // 插件
import './util/rem' // rem
// 此处引入 是为了给全局加上版本号 方便后期在浏览器中直接看到版本号
import { version } from '../package.json'

document.oldWrite = document.write
document.write = null
console.log('当前语言', store.state.language)
// 挂载版本号
window.version = version

// eslint-disable-next-line no-undef
Vue.use(uw)

Vue.use(Lazyload, {
  error: require('./assets/images/imgNone2.png'),
  loading: require('./assets/images/loading.gif'),
  attempt: 1,
  lazyComponent: true
})
Vue.use(VueLazyLoad, {
  preLoad: 1.3,
  error: require('./assets/images/imgNone2.png'),
  loading: require('./assets/images/loading.gif'),
  attempt: 1
})
Vue.use(plugin)
Vue.config.ignoredElements = ['wx-open-launch-weapp']

if (browser().ios) {
  window._IOSsignatureURL = window.location.href
}

// 进入系统记录一次登陆(前提是发起登陆的情况)
if (!sessionStorage.getItem('enter')) {
  sessionStorage.setItem('enter', location.href)
}

try {
  document.querySelector('._loading').setAttribute('style', 'display: none;')
} catch {
  console.info("document.querySelector('._loading')  is not defined")
}

// 进入系统，如果当前系统没有信息和url地址栏的信息不一样--就直接跳转到登录页 -- end
router.beforeEach((to, from, next) => {
  next()
})
router.afterEach((to, from) => {

})

// 渲染一个路由的过程中，需要尝试解析一个异步组件时发生错误
router.onError(error => {
  const pattern = /(chunk|app)-\w+\.\w+\.[0-9]+\.js/g
  const isChunkLoadFailed = error.message.match(pattern)
  const targetPath = router.history.pending.fullPath
  if (isChunkLoadFailed) {
    router.replace(targetPath)
  }
})

/**
 * Vue实例
 */
const app = new Vue({
  router,
  store,
  i18n,
  render: h => h(App)
}).$mount('#app')

export default app
